
// 从url中获取当前的是价格升序还是降序
var psort = GetQueryString("psort");
if (psort == '' || psort != 0) {
    psort = 1;
}
else {
    psort = 0;
    $("#priceArrow").removeClass("uk-icon-long-arrow-up");
    $("#priceArrow").addClass("uk-icon-long-arrow-down");
}

var keyword = GetQueryString("keyword");
if (keyword != '') {
    var tip = " <strong>当前搜索关键字： </strong>" + keyword + "<a href=\"index.html\" class=\"uk-close\"></a>";
    $("#searchtip").append(tip);
    $("#searchkeyword").val(keyword);
}

// 加载商品列表
function getGoods(pageSize, page) {
    var url;
    if (keyword == null || keyword == '') {
        url = GetBaseUrl() + "goods/" + pageSize + "/" + page + "/" + psort; // 正常加载
    }
    else {
        url = GetBaseUrl() + "goods/" + pageSize + "/" + page + "/" + psort + "/" + keyword; // 搜索商品
    }

    $.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        success: function (result) {
            var result;
            if (result.code != 0) {
                alert("获取商品列表失败：" + result.msg);
            }
            else {
                var pagination = UIkit.pagination($("#paginatiom"), { items: result.data.total, itemsOnPage: result.data.pageSize });

                $("#goods-list").empty();
                $.each(result.data.list, function (i, item) {

                    // $("#goods-list").append("<li>" + item.name + "</li>")

                    var str = "<li class=\"goods-preview-li\">" + "<div class=\"goods-preview-img\">"
                        + "<a  href=\"//item.jd.com/4359542.html\">"
                        + "<img alt=\"" + item.name + "\"   src=\"img/goods/" + item.previewimg + "\" title=\"" + item.name + "\"> </a>"
                        + "</div> <div class=\"goods-preview-name\" >"
                        + "<a target=\"_blank\" href=\"//item.jd.com/4359542.html\" title=\"" + item.name + "\">" + item.name + "</a>"
                        + "</div><div class=\"goods-preview-price\">优惠价：<strong >¥<em >" + item.price + "</em></strong>"
                        + "<a target=\"_blank\" class=\"uk-button-success buy-btn\" href=\"//item.jd.com/4359542.html\" class=\"buy-btn\">抢购</a> </div> </li>"
                    $("#goods-list").append(str)
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("服务异常");
        }
    })
}

// 翻页操作
$("#paginatiom").on('select.uk.pagination', function (e, pageIndex) {
    pageIndex = pageIndex + 1;
    eval("getGoods(6," + pageIndex + ")") //执行函数调用  
});


// 页面加载后加载商品列表
$(document).ready(
    eval("getGoods(6, 1)") //执行函数调用  
);


// 商品排序
function sortGoods() {
    if (psort == 1) {
        psort = 0;
    }
    else {
        psort = 1;
    }

    var url = "index.html?psort=" + psort;
    if (keyword != null && keyword != '') {
        url = url + "&keyword=" + keyword;
    }

    window.location.href = url;
}